/*
 *INFO:
 *	to implement some operation with the buffer_queue that store the packet
 *
 *Author:
 *	mark on 2013/12/--
 *	<mark199312@gmail.com>
 */


/*
 *code log:
 *2014/3/23 to add the record_key_t to the struct
 */


#include<stdio.h>
#include"buff_queue.h"

/*
 *to init the struct 
 */

void buff_queue_init(struct buff_queue **buffer_queue)
{
	if(*buffer_queue==NULL)
	{
		(*buffer_queue)=(struct buff_queue*)malloc(sizeof(struct buff_queue));
	}


	(*buffer_queue)->count=0;
	(*buffer_queue)->head=NULL;
	(*buffer_queue)->tail=NULL;
}

/*
 *add the node to the queue struct
 */

void buff_queue_add(struct buff_queue * buffer_queue,struct buff_queue_node *node)
{
	struct buff_queue_node * tail;
	struct buff_queue_node * current=buffer_queue->tail;

	if(current==NULL)
	{
		log_info("add and buffer is empty");
		current=node;
		buffer_queue->head=node;
		buffer_queue->tail=node;
		
		buffer_queue->count++;
		printf("success add to queue %d\n",buffer_queue->count);
		return;
	}
/*
	while(current->next!=NULL)
	{
		current=current->next;
	}
*/	
	current->next=node;

	buffer_queue->tail=node;

	buffer_queue->count++;
	
	printf("success add to queue %d\n",buffer_queue->count);
}

/*
 *delete the node from the bueue like the pop
 */

struct buff_queue_node* buff_queue_del(struct buff_queue * buffer_queue)
{
	struct buff_queue_node * current=buffer_queue->head;

	struct buff_queue_node * next;
	
	if(current==NULL)
	{
		log_error("del and buffer is empty");
		return NULL;
	}
	
	next=current->next;
	buffer_queue->head=next;

	if(buffer_queue->head==NULL)
	{
		buffer_queue->tail=NULL;
	}

	buffer_queue->count--;
	printf("now you are in the buff_queue_del and the count are :%d\n",buffer_queue->count);
	return current;
}

/*
 *count the packet node number
 */
int buff_queue_count(struct buff_queue *buffer_queue)
{
	return buffer_queue->count;
}


